package com.uzai.console.service.fd.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.*;
import com.uzai.console.dto.fd.fdsendjob.FdSendJobDto;
import com.uzai.console.entity.*;
import com.uzai.console.jsonvo.fd.fdtemplate.FdTemplateData;
import com.uzai.console.mapper.*;
import com.uzai.console.service.fd.FdAutoSendService;
import com.uzai.console.service.fd.FdSendJobSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 发单自动发送
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class FdAutoSendServiceImpl implements FdAutoSendService {

    Logger logger = LoggerFactory.getLogger(FdAutoSendServiceImpl.class);

    @Autowired
    private FdAutoConfigMapper fdAutoConfigMapper;
    @Autowired
    private FdDevicePlanMapper fdDevicePlanMapper;
    @Autowired
    private FdDevicePlanGroupMapper fdDevicePlanGroupMapper;
    @Autowired
    private WxqGroupManagePlanMapper wxqGroupManagePlanMapper;
    @Autowired
    private FdSendJobMapper fdSendJobMapper;
    @Autowired
    private FdSendJobSyncService fdSendJobSyncService;

    /**
     * 自动发送发单
     * @author lucher
     * @date 2022/9/19 14:05
     * @param fdAutoConfig
     */
    public void autoSendFd(FdAutoConfig fdAutoConfig) {

        if (fdAutoConfig == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "自动发单配置不存在");
        }

        if (fdAutoConfig.getFdDevicePlanId() == null) {
            throw new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择设备方案");
        }

        FdDevicePlan fdDevicePlan = fdDevicePlanMapper.selectById(fdAutoConfig.getFdDevicePlanId(), fdAutoConfig.getMerId());
        if (fdDevicePlan == null) {
            throw new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择的设备方案不存在");
        }

        //查询该配置方案关联的群信息
        List<FdDevicePlanGroup> fdDevicePlanGroupList = fdDevicePlanGroupMapper.findByListByFdDevicePlanId(fdDevicePlan.getId(), fdDevicePlan.getMerId());
        if (fdDevicePlanGroupList == null || fdDevicePlanGroupList.size() == 0) {
            throw new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该设备方案未配置任何群信息");
        }

        //查询所有发送内容
        List<FdTemplateData> fdTemplateDataList = JSONObject.parseArray(fdAutoConfig.getData(), FdTemplateData.class);
        if (fdTemplateDataList == null || fdTemplateDataList.size() == 0) {
            throw new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入发送内容");
        }

        //主任务
        int now = DateUtil.getNowTime();
        FdSendJob fdSendJob_parent = new FdSendJob();
        fdSendJob_parent.setId(IdWorker.getId());
        fdSendJob_parent.setParentId(0L);
        fdSendJob_parent.setMerId(fdAutoConfig.getMerId());
        fdSendJob_parent.setName("自动发群");
        fdSendJob_parent.setSource(1); //自动发群
        fdSendJob_parent.setFdDevicePlanId(fdAutoConfig.getFdDevicePlanId());
        fdSendJob_parent.setStatus(0); //未开始
        fdSendJob_parent.setData(fdAutoConfig.getData());
        fdSendJob_parent.setTotalCount(0);
        fdSendJob_parent.setFinishCount(0);
        fdSendJob_parent.setFinishCountSnapshot(0);
        fdSendJob_parent.setBeginTime(fdAutoConfig.getSendTime());
        fdSendJob_parent.setExptime(fdAutoConfig.getSendTime() + 7 * 24 * 3600);
        fdSendJob_parent.setCreateTime(now);
        fdSendJob_parent.setUpdateTime(now);
        fdSendJob_parent.setBeginTime(fdAutoConfig.getSendTime());
        fdSendJob_parent.setSendStartTime(fdAutoConfig.getSendTime() + 60); //在开始时间之后60秒发送
        fdSendJobMapper.insertSelective(fdSendJob_parent);

        //封装子任务
        //封装所有群(子任务)
        List<FdSendJob> fdSendJobList_sub = new ArrayList<>();
        for (FdDevicePlanGroup fdDevicePlanGroup : fdDevicePlanGroupList) {
            FdSendJob fdSendJob_sub = new FdSendJob();
            fdSendJob_sub.setId(IdWorker.getId());
            fdSendJob_sub.setParentId(fdSendJob_parent.getId());
            fdSendJob_sub.setMerId(fdSendJob_parent.getMerId());
            fdSendJob_sub.setName(fdSendJob_parent.getName());
            fdSendJob_sub.setSource(fdSendJob_parent.getSource());
            fdSendJob_sub.setFdDevicePlanId(fdSendJob_parent.getFdDevicePlanId());
            fdSendJob_sub.setStatus(0); //未开始
            fdSendJob_sub.setData(fdSendJob_parent.getData());
            fdSendJob_sub.setTotalCount(0);
            fdSendJob_sub.setFinishCount(0);
            fdSendJob_sub.setFinishCountSnapshot(0);
            fdSendJob_sub.setBeginTime(fdSendJob_parent.getBeginTime());
            fdSendJob_sub.setExptime(fdSendJob_parent.getBeginTime() + 15 * 24 * 3600);
            fdSendJob_sub.setCreateTime(now);
            fdSendJob_sub.setUpdateTime(now);
            fdSendJob_sub.setBeginTime(fdSendJob_parent.getBeginTime());
            fdSendJob_sub.setSendStartTime(fdSendJob_parent.getSendStartTime());
            //群发机器人消息
            fdSendJob_sub.setDeviceType(fdDevicePlanGroup.getDeviceType());
            fdSendJob_sub.setGroupId(fdDevicePlanGroup.getGroupId());
            fdSendJob_sub.setNick(fdDevicePlanGroup.getNick());
            fdSendJob_sub.setDeviceUniqueId(fdDevicePlanGroup.getDeviceUniqueId());
            fdSendJob_sub.setDeviceId(fdDevicePlanGroup.getDeviceId());
            fdSendJob_sub.setWxid(fdDevicePlanGroup.getWxid());
            fdSendJob_sub.setGroupSendDeviceUniqueId(fdDevicePlanGroup.getGroupSendDeviceUniqueId());
            fdSendJobList_sub.add(fdSendJob_sub);
        }

        FdSendJobDto fdSendJobDto = new FdSendJobDto();
        fdSendJobDto.setMerId(fdAutoConfig.getMerId());
        fdSendJobDto.setBeginTime(fdAutoConfig.getSendTime());
        fdSendJobDto.setFdDevicePlanId(fdAutoConfig.getFdDevicePlanId());
        fdSendJobDto.setFdTemplateDataList(fdTemplateDataList);
        fdSendJobDto.setName(fdAutoConfig.getName());

        fdSendJobSyncService.addFdSendJobTaskSync(fdSendJobDto, fdDevicePlan, fdDevicePlanGroupList, fdSendJobList_sub);

        //添加成功才保持
        //修改最新的发送时间(放到发送完成之后才保存)
        fdAutoConfigMapper.updateSendTimeById(fdAutoConfig);
    }

}
